package com.tt.lcd.qunyangGPRS.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.tt.lcd.qunyangGPRS.R;
import com.tt.lcd.qunyangGPRS.aSDK.SDKserver;
import com.tt.lcd.qunyangGPRS.biz.activity.renshen.LogInfo;
import com.tt.lcd.qunyangGPRS.biz.activity.renshen.QunyangInfo;
import com.tt.lcd.qunyangGPRS.dao.MyUser;
import com.tt.lcd.qunyangGPRS.dao.renshen.XiaoqunyangDao;
import com.tt.lcd.qunyangGPRS.systemActivity.UserActivity;
import com.tt.lcd.qunyangGPRS.utils.MyApplication;
import com.tt.lcd.qunyangGPRS.utils.Register;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Qunyang_Info extends Activity {
	private TextView zhanshu, pigs, add, chaxun, dangtian;
	ListView listView;
	IntentFilter intf;

	private SDKserver server;

	private ServiceConnection scon = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// server 不正常结束时回调
			Log.e("", "服务非正常结束");
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// 回调函数
			Log.e("", "onServiceConnected");
			server = ((SDKserver.LocalBinder) service).getServeice();
			Register.getRegister().server = server;

		}
	};

	private BroadcastReceiver br = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getStringExtra("key2").equals("OK")) {
				onResume();
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		XiaoqunyangDao x = new XiaoqunyangDao(this);
		x.create();
		// 如果没注册跳转到注册界面
//		if (new MyUser(Qunyang_Info.this).selectUID().equals("")) {
//			Intent it = new Intent();
//			it.setClass(Qunyang_Info.this, UserActivity.class);
//			startActivity(it);
//		}

		// 启动ASDK服务
		Intent it = new Intent(this, SDKserver.class);
		bindService(it, scon, BIND_AUTO_CREATE);


		intf = new IntentFilter("com.lcd.qydangtianRec");// 参数过滤器筛选条件，可以理解为广播名称
		registerReceiver(br, intf); // 注册广播
		setContentView(R.layout.qunyang_info);



		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}

				if (Register.getRegister().server!=null) {
					try {
						new QunyangInfo(Qunyang_Info.this).send();
					} catch (ParseException e) {
						Toast.makeText(getApplicationContext(), "时间格式错误，查询失败",
								Toast.LENGTH_SHORT).show();
					}
					try {
						new LogInfo(Qunyang_Info.this).send();
					} catch (ParseException e) {
						Toast.makeText(getApplicationContext(), "时间格式异常", Toast.LENGTH_SHORT)
								.show();
					}
				}
			}
		}).start();


		zhanshu = (TextView) findViewById(R.id.zhanshu);
		pigs = (TextView) findViewById(R.id.muzhushu);
		add = (TextView) findViewById(R.id.qy_add);
		chaxun = (TextView) findViewById(R.id.qy_chaxun);
		chaxun.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				try {
					new QunyangInfo(Qunyang_Info.this).send();
					Toast.makeText(Qunyang_Info.this, "执行查询，等待回复", Toast.LENGTH_SHORT).show();
				} catch (ParseException e) {
					e.printStackTrace();
					Toast.makeText(getApplicationContext(), "时间格式错误，查询失败", Toast.LENGTH_SHORT)
							.show();
				}
			}
		});

		dangtian = (TextView) findViewById(R.id.qy_dangtain);
		dangtian.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent it = new Intent();
				it.setClass(Qunyang_Info.this, QyDaitianZhanInfo.class);

				startActivity(it);
			}
		});

		add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent it = new Intent();
				it.setClass(Qunyang_Info.this, AddFeeder.class);

				startActivity(it);
			}
		});

		listView = (ListView) this.findViewById(R.id.siweizhan_list);

		zhanshu.setText("" + new QunyangInfo(Qunyang_Info.this).getZhanCout());

		pigs.setText("" + new QunyangInfo(Qunyang_Info.this).getPigCout());

		// 创建SimpleAdapter适配器将数据绑定到item显示控件上
		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		try {
			data = new QunyangInfo(Qunyang_Info.this).getData();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		SimpleAdapter adapter = new SimpleAdapter(this, data,
				R.layout.siweizhan_listview, new String[] { "zhanId",
				"muhzuzongshu", "muzhu_shuliang" }, new int[] {
				R.id.zhanID, R.id.muzhuzongshu, R.id.muzhu_shuliang });
		// 实现列表的显示
		listView.setAdapter(adapter);
		// 条目点击事件
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
									int position, long id) {
				ListView listView = (ListView) parent;
				HashMap<String, Object> data = (HashMap<String, Object>) listView
						.getItemAtPosition(position);

				String muzhushu = data.get("muzhu_shuliang").toString();

				if (!muzhushu.equals("失联") && !muzhushu.equals("数据未更新")) {
					String zhanId = data.get("zhanId").toString();
					Intent it = new Intent();
					it.setClass(Qunyang_Info.this, QyDangtianInfo.class);
					//
					// Bundle bundle = new Bundle();
					// bundle.putString("zhanId", date);
					// it.putExtras(bundle);
					it.putExtra("zhanId", zhanId);
					startActivity(it);
				}

			}

		});

	}

	@Override
	protected void onResume() {
		super.onResume();

		zhanshu.setText("" + new QunyangInfo(Qunyang_Info.this).getZhanCout());

		pigs.setText("" + new QunyangInfo(Qunyang_Info.this).getPigCout());

		// 创建SimpleAdapter适配器将数据绑定到item显示控件上
		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		try {
			data = new QunyangInfo(Qunyang_Info.this).getData();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		SimpleAdapter adapter = new SimpleAdapter(this, data,
				R.layout.siweizhan_listview, new String[] { "zhanId",
				"muhzuzongshu", "muzhu_shuliang" }, new int[] {
				R.id.zhanID, R.id.muzhuzongshu, R.id.muzhu_shuliang });
		// 实现列表的显示
		listView.setAdapter(adapter);
		// 条目点击事件
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
									int position, long id) {
				ListView listView = (ListView) parent;
				HashMap<String, Object> data = (HashMap<String, Object>) listView
						.getItemAtPosition(position);

				String muzhushu = data.get("muzhu_shuliang").toString();

				if (!muzhushu.equals("失联") && !muzhushu.equals("数据未更新")) {
					String zhanId = data.get("zhanId").toString();
					Intent it = new Intent();
					it.setClass(Qunyang_Info.this, QyDangtianInfo.class);
					//
					// Bundle bundle = new Bundle();
					// bundle.putString("zhanId", date);
					// it.putExtras(bundle);
					it.putExtra("zhanId", zhanId);
					startActivity(it);
				}
			}
		});

	}

	@Override
	protected void onDestroy() {
		unbindService(scon);
		unregisterReceiver(br);
		super.onDestroy();
	}

}
